﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;

namespace QuickModel.InteractiveOperation
{
	internal class DwgEntitiesSelectFilter : ISelectionFilter
	{
		public DwgEntitiesSelectFilter(Autodesk.Revit.DB.Document doc, SelecteEntType dwgEntType = SelecteEntType.eAll, List<ElementId> lstLayer = null)
		{
			this.m_doc = doc;
			this.m_entType = dwgEntType;
			this.m_lstLayer = lstLayer;
		}

		public bool AllowElement(Element elem)
		{
			return true;
		}

		public bool AllowReference(Reference reference, XYZ position)
		{
			bool result;
			try
			{
				Element element = this.m_doc.GetElement(reference);
				ImportInstance importInstance = element as ImportInstance;
				bool flag = importInstance == null;
				if (flag)
				{
					result = false;
				}
				else
				{
					Element element2 = this.m_doc.GetElement(importInstance.GetTypeId());
					bool flag2 = !(element2 is CADLinkType);
					if (flag2)
					{
						result = false;
					}
					else
					{
						CADLinkType cadlinkType = element2 as CADLinkType;
						GeometryElement geometryElement = cadlinkType.get_Geometry(new Options());
						GeometryObject geometryObjectFromReference = element.GetGeometryObjectFromReference(reference);
						bool flag3 = geometryObjectFromReference.GraphicsStyleId == geometryElement.GraphicsStyleId;
						if (flag3)
						{
							result = false;
						}
						else
						{
							bool flag4 = !this.IsInTargetLayer(geometryObjectFromReference.GraphicsStyleId);
							if (flag4)
							{
								result = false;
							}
							else
							{
								result = DwgEntitiesSelectFilter.IsSpecifiedType(geometryObjectFromReference, this.m_entType);
							}
						}
					}
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static bool IsSpecifiedType(GeometryObject geoObj, SelecteEntType entType)
		{
			bool flag = (entType & SelecteEntType.eAll) == SelecteEntType.eAll;
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				bool flag2 = (entType & SelecteEntType.eArc) == SelecteEntType.eArc && geoObj is Arc;
				if (flag2)
				{
					result = true;
				}
				else
				{
					bool flag3 = (entType & SelecteEntType.eGeoEle) == SelecteEntType.eGeoEle && geoObj is GeometryElement;
					if (flag3)
					{
						result = true;
					}
					else
					{
						bool flag4 = (entType & SelecteEntType.eGeoIns) == SelecteEntType.eGeoIns && geoObj is GeometryInstance;
						if (flag4)
						{
							result = true;
						}
						else
						{
							bool flag5 = (entType & SelecteEntType.eLine) == SelecteEntType.eLine && geoObj is Line;
							if (flag5)
							{
								result = true;
							}
							else
							{
								bool flag6 = (entType & SelecteEntType.eNurbspline) == SelecteEntType.eNurbspline && geoObj is NurbSpline;
								if (flag6)
								{
									result = true;
								}
								else
								{
									bool flag7 = (entType & SelecteEntType.ePolyLine) == SelecteEntType.ePolyLine && geoObj is PolyLine;
									if (flag7)
									{
										result = true;
									}
									else
									{
										bool flag8 = (entType & SelecteEntType.eText) == SelecteEntType.eText && geoObj.GetType().ToString() == "Autodesk.Revit.DB.GeometryObject";
										result = flag8;
									}
								}
							}
						}
					}
				}
			}
			return result;
		}

		private bool IsCurve(GeometryObject geoObj)
		{
			return geoObj is Line || geoObj is NurbSpline || geoObj is Arc || geoObj is CylindricalHelix || geoObj is Ellipse || geoObj is HermiteSpline || geoObj is PolyLine;
		}

		private bool IsInTargetLayer(ElementId graphicsStyleId)
		{
			try
			{
				bool flag = this.m_lstLayer.IsEmpty<ElementId>();
				if (flag)
				{
					return true;
				}
				GraphicsStyle graphicsStyle = this.m_doc.GetElement(graphicsStyleId) as GraphicsStyle;
				bool flag2 = this.m_lstLayer.Contains(graphicsStyle.GraphicsStyleCategory.Id);
				if (flag2)
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private Document m_doc = null;

		private SelecteEntType m_entType = SelecteEntType.eAll;

		private List<ElementId> m_lstLayer = new List<ElementId>();
	}
}
